package com.ruiyun.ruinews.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ruiyun.ruinews.R;
import com.ruiyun.ruinews.activity.MainActivity;
import com.ruiyun.ruinews.adapter.NewsAdapter;
import com.ruiyun.ruinews.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zouhui on 16/1/7.
 */
public class NewsFragment extends BaseFragment {
    private NewsAdapter adapter;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<Fragment> data;
    private List<String> title;
    private Toolbar toolbar;
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        init(view);
        return view;
    }

    /**
     * 加载控件
     *
     * @param view
     */
    private void init(View view) {
        data = new ArrayList<>();
        title = new ArrayList<>();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar_news);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager_news);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout_news);
        toolbar.setTitle("睿新闻");
//        toolbar.sett
        toolbar.setMinimumWidth(40);
        String[] currentTitle = {"头条", "娱乐", "热点", "体育", "大连", "订阅", "财经", "科技", "汽车", "时尚",
                "房产", "军事", "历史", "家居", "原创", "健康", "政务"};
        for (int i = 0; i < currentTitle.length; i++) {
            data.add(new NewsClassFragment(currentTitle[i]));
            title.add(currentTitle[i]);
        }
        adapter = new NewsAdapter(getActivity().getSupportFragmentManager(), data, title);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTab(i));
            }
        }
        viewPager.setCurrentItem(0);
        setHasOptionsMenu(true);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((MainActivity) getActivity()).setSupportActionBar(toolbar);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_news, menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.allDay_item_news_toolbar:
                Toast.makeText(context, "24小时新闻", Toast.LENGTH_SHORT).show();
                break;
            case R.id.searche_item_news_toolbar:
                Toast.makeText(context, "搜索", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
